<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

function createInstance($data = array()) {

    $return = "<script type=\"text/javascript\">
     	CKEDITOR.replace('" . $data['id'] . "', {";

    //Adding config values
    if (isset($data['config'])) {
        foreach ($data['config'] as $k => $v) {

            // Support for extra config parameters
            if (is_array($v)) {
                $return .= $k . " : [";
                $return .= configData($v);
                $return .= "]";
            } else {
                $return .= $k . " : '" . $v . "'";
            }

            if ($k !== end(array_keys($data['config']))) {
                $return .= ",";
            }
        }
    }

    $return .= '});</script>';

    return $return;
}

function configData($data = array()) {
    $return = '';
    foreach ($data as $key) {
        if (is_array($key)) {
            $return .= "[";
            foreach ($key as $string) {
                $return .= "'" . $string . "'";
                if ($string != end(array_values($key)))
                    $return .= ",";
            }
            $return .= "]";
        }
        else {
            $return .= "'" . $key . "'";
        }
        if ($key != end(array_values($data)))
            $return .= ",";
    }
    return $return;
}